package first.endtoend.parse

import first.endtoend.Category
import org.springframework.transaction.annotation.Transactional


/*
@author : A.Olfa
 */

@Transactional
class ParseCSVCategoryService
{

    def Category categoryInstance

    def parseCategories(String filepath, boolean header)
    {
        println("####################### JE PASSE DANS PARSE CATEGORIES #############")
        println("####################### PATH = " + filepath + " #############")
        def parse = new File(filepath).readLines()[1..-1]
        println("####################### APRES FILE #######################")

        def token = ';'
        println("####################### AVANT BOUCLE FOR ######################")
        for (line in parse)
        {
            // create a categoryInstance and specify its properties

            line.splitEachLine(token) { fields ->

                categoryInstance = new Category(name: fields[0])


                categoryInstance.save()

                /* if (categoryInstance.hasErrors()){
                     println categoryInstance.errors
                 }*/


            }

        }
        println("####################### SORTIT PARSE CATEGORIES #######################")
    }
}